package js.dwim.server;

import java.util.Date;
import java.util.List;

import javax.jdo.JDOHelper;
import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;
import javax.jdo.Query;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.xmpp.JID;
import com.google.appengine.api.xmpp.Message;

import js.dwim.domain.CommSession;
import js.dwim.domain.Player;

public final class Persistence
{
    private static final PersistenceManagerFactory pmfInstance = JDOHelper
            .getPersistenceManagerFactory( "transactions-optional" );

    private Persistence()
    {
    }

    public static PersistenceManager getManager()
    {
        return pmfInstance.getPersistenceManager();
    }
    
	public static <T> T getObjectByKey( Class<T> arg0, Key key )
    {
		PersistenceManager pm = Persistence.getManager();

		try
		{
	    	return pm.detachCopy( pm.getObjectById( arg0, key ) );
		}
		finally
		{
			pm.close();
		}
    }

    public static CommSession getSession( Message msg )
    {
        JID sender                      = msg.getFromJid();
        PersistenceManager pm           = Persistence.getManager();
        CommSession session, detached   = null;

        try
        {
            Query q = pm.newQuery( CommSession.class, "jid == sender" );
            q.declareParameters( "java.lang.String sender" );

            @SuppressWarnings( "unchecked" )
            List<CommSession> result = (List<CommSession>) q.execute( sender.getId() );
            if ( !result.isEmpty() )
            {
                session     = result.get( 0 );
                detached    = pm.detachCopy( session );
            }
            else
            {
                session = new CommSession( sender, new Player( sender.getId() ) );
                pm.makePersistent( session );
                detached = pm.detachCopy( session );
            }
        }
        finally
        {
            pm.close();
        }
        return detached;
    }

    public static void updateSession( CommSession session )
    {
        PersistenceManager pm = Persistence.getManager();
        try
        {
            session.setLastActivityDate( new Date() );
            pm.makePersistent( session );
        }
        finally
        {
            pm.close();
        }
    }
    
    public static void store( Object obj )
    {
        PersistenceManager pm = Persistence.getManager();
        try
        {
            pm.makePersistent( obj );
        }
        finally
        {
            pm.close();
        }
    }

    public static void deletePersistent( Object obj )
    {
        PersistenceManager pm = Persistence.getManager();
        try
        {
            pm.deletePersistent( obj );
        }
        finally
        {
            pm.close();
        }
    }

    public static void deleteAllPersistent()
    {
    }
}